package com.itqf.controller;

import com.alibaba.fastjson.JSONObject;
import com.itqf.service.DashboardService;
import com.itqf.service.DtsOrderService;
import com.itqf.service.DtsUserService;
import com.itqf.util.ResponseUtil;
import com.itqf.vo.CategorySellAmts;
import com.itqf.vo.DayStatis;
import com.itqf.vo.OrderAmtsVo;
import com.itqf.vo.UserOrderCntVo;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * projectName: sm_project
 *
 * @author: 赵伟风
 * time: 2021/7/14 15:29
 * description: 首页数据展示controller
 */

@RestController
@CrossOrigin("*")
@RequestMapping("admin/dashboard")
public class DashBoardController {

    @Autowired
    private DashboardService dashboardService;

    @Autowired
    private DtsOrderService dtsOrderService;

    @Autowired
    private DtsUserService dtsUserService;

    public static final Logger logger  = LoggerFactory.getLogger(DashBoardController.class);


    @GetMapping("")
    public Object show(){

        logger.info("[首页数量]数量展示接口开始调用!!");

        Map data = dashboardService.showDashboard();

        logger.debug("[首页数量]查询数据为:{}", data);

        return ResponseUtil.ok(data);
    }



    @GetMapping("chart")
    public Object chart(@RequestParam(defaultValue = "30") int days){

        //装数据的总map
        Map data = new HashedMap();

        //1.类别销售额
        List<CategorySellAmts> categorySellAmts = dtsOrderService.queryOrderSell(days);
        logger.debug("[图表接口]类别销售额:{}!", categorySellAmts);
        String [] categoryNames = new String[categorySellAmts.size()];
        //类别转到数组中
        for (int i = 0; i < categorySellAmts.size(); i++) {
            categoryNames[i] = categorySellAmts.get(i).getName();
        }
        Map categorySell = new HashedMap();
        categorySell.put("categoryNames",categoryNames);
        categorySell.put("categorySellData", categorySellAmts);

        logger.debug("[图标接口]类别销售额最后封装的数据为:{}", categorySell);

        data.put("categorySell",categorySell);

        //2.订单销售额和订单订单数量
        //查询每天的订单金额  订单数量和订单时间
        List<DayStatis> dayStatis =  dtsOrderService.queryDayOrderCell(days);

        OrderAmtsVo orderAmtsVo = new OrderAmtsVo();

        String [] dayData = new String[dayStatis.size()];
        BigDecimal [] orderAmtData = new BigDecimal[dayStatis.size()];
        int [] orderCntData = new int[dayStatis.size()];

        for (int i = 0; i < dayStatis.size(); i++) {
            DayStatis ds = dayStatis.get(i);
            dayData[i] = ds.getDayStr();
            orderAmtData[i]=ds.getAmts();
            orderCntData[i]=ds.getCnts();
        }

        orderAmtsVo.setDayData(dayData);
        orderAmtsVo.setOrderAmtData(orderAmtData);
        orderAmtsVo.setOrderCntData(orderCntData);

        logger.debug("[图标接口] 查询每日销售额的的数据:{}", orderAmtsVo);

        data.put("orderAmts",orderAmtsVo);


        //3.查询用户和订单增长量
        List<DayStatis> userStatis = dtsUserService.queryDayUser(days);

        UserOrderCntVo userOrderCnt = new UserOrderCntVo();

        Set<String> dayDataTime = new HashSet<>();
        //用户增长量
        for (DayStatis userStati : userStatis) {
            dayDataTime.add(userStati.getDayStr());
        }
        //订单增长量
        for (DayStatis dayStati : dayStatis) {
            dayDataTime.add(dayStati.getDayStr());
        }
        //将时间set转成数组存储到数据中
        userOrderCnt.setDayData(dayDataTime.toArray(new String[0]));

        //定义用户增长数组
        int [] userCnt = new int[dayDataTime.size()];

        //并集的time
        int index = 0;
        for (String time : dayDataTime) {

            int number = 0;
            //每日的数据
            for (DayStatis stati : userStatis) {
                if (stati.getDayStr().equals(time)) {
                    //如果有时间,赋时间!
                    number = stati.getCnts();
                    break;
                }
            }

            userCnt[index] = number;
            index++;
        }


        userOrderCnt.setUserCnt(userCnt);

        //订单的数量
        int [] orderCnt = new int[dayDataTime.size()];

        //重置index
        index = 0;
        for (String time : dayDataTime) {

            int number = 0;
            //订单每日的数据
            for (DayStatis stati : dayStatis) {
                if (stati.getDayStr().equals(time)) {
                    //如果有时间,赋时间! 订单增长量
                    number = stati.getCnts();
                    break;
                }
            }

            orderCnt[index] = number;
            index++;
        }

        userOrderCnt.setOrderCnt(orderCnt);

        data.put("userOrderCnt",userOrderCnt);

        logger.info("[图标接口]完整数据如下:{}", JSONObject.toJSONString(data));

        return ResponseUtil.ok(data);
    }

}
